﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Web;
using System.Web.Mvc;
using IntellectShop.Web2.ViewModels;

namespace IntellectShop.Web2.Controllers
{
	[IntellectShop.Web2.Code.NoCache]
	[IntellectShop.Web2.Code.HandleErrorWithELMAH]
	public class HomeController : Controller
	{
		public ActionResult Index()
		{
			ViewData["Message"] = "Welcome to ASP.NET MVC!";

			return View();
		}

		public ActionResult BranchesOfLearning()
		{
			return View();
		}

		public ActionResult Managers()
		{
			return View();
		}

		public ActionResult AboutCompany()
		{
			return View();
		}

		public ActionResult About()
		{
			var viewModel = new FeedbackEditViewModel { Feedback = new Common.Feedback() };
			return View(viewModel);
		}

		[HttpPost]
		public ActionResult About(Common.Feedback feedback)
		{
			try
			{
				if (!ModelState.IsValid)
					throw new ArgumentException("Ошибка при заполнении полей для: Feedback!");

				feedback.FeedbackOwnerID = Common.Globals.GetOwnerID(this.HttpContext);
				feedback.Create(this.HttpContext.User.Identity.Name);

				var viewModel = new FeedbackEditViewModel {
					Feedback = new Common.Feedback(),
					Message = String.Format("Ваше сообщение успешно отправлено в {0}.", DateTime.Now)
				};
				return View(viewModel);
			}
			catch (Exception e)
			{
				var viewModel = new FeedbackEditViewModel
				{
					Feedback = new Common.Feedback(),
					Message = e.Message
				};
				return View(viewModel);
			}
		}

		public ActionResult OurPartners()
		{
			return View();
		}

		public ActionResult OurClients()
		{
			return View();
		}

		public ActionResult References()
		{
			return View();
		}

		public VCardResult vCard()
		{
			var vCard = new Common.VCard
			{
				FirstName = "Pavel",
				LastName = "Osadchiy",
				Organization = "AtriLab",
				JobTitle = "Software developer",
				StreetAddress = "Lenin, 83-212",
				City = "Chelyabinsk",
				CountryName = "Russia",
				Phone = "",
				Mobile = "",
				Email = "atri24@gmail.com",
				HomePage = "atri15.blogspot.com",
				Image = System.IO.File.ReadAllBytes(Server.MapPath(@"../Content/Images/me.jpg"))
			};

			return new VCardResult(vCard);
		}

		public ActionResult Image(string imagename)
		{
			/// тут надо картинку поднимать из базы
			using (Image image = Bitmap.FromFile(imagename))
			{
				if (image == null)
				{
					throw new HttpException(404, "Not Found");
				}

				using (MemoryStream ms = new MemoryStream())
				{
					image.Save(ms, ImageFormat.Gif);
					return File(ms.ToArray(), "image/gif");
				}
			}
		}
	}
}
